package character

import "crypto/rand"

func GenerateRandom(length int) (string, error) {
	// 字符集
	charset := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
	// 用于保存生成的密码
	var str []rune
	// 密码长度
	passwordLength := length
	// 随机数生成器
	randomNumberGenerator := rand.Reader

	for i := 0; i < passwordLength; {
		b := make([]byte, 1)
		_, err := randomNumberGenerator.Read(b)
		if err != nil {
			return "", err
		}
		// 从字符集中选取字符
		n := int(charset[int(b[0])%len(charset)])
		str = append(str, rune(n))
		// 记录已经选取的字符数
		i++
	}

	return string(str), nil
}

func GetHashGenerateRandom(length int) (string, string, error) {
	randomStr, err := GenerateRandom(length)
	if err != nil {
		return "", "", err
	}

	hashStr, err := Hash(randomStr)
	if err != nil {
		return "", "", err
	}

	return randomStr, hashStr, nil
}
func GetHashGenerateCRandom(randomStr string) (string, error) {

	hashStr, err := Hash(randomStr)
	if err != nil {
		return "", err
	}

	return hashStr, nil
}
